import courseApi from "../../api/chaoxing/courseApi/index";
import signActivity from "../../api/chaoxing/signActivity/index";
import userApi from "../../api/chaoxing/userApi/index"
import dbUserApi from "../../api/wechatCloud/dbUserApi/index";
import showToast from "../../utils/toast";
// 获取应用实例
Page({
  data: {
    userName: '',
    scanCode: '扫码',
  },
  // 事件处理函数
  bindGotoBtn() {
    wx.navigateTo({
      url: '../course/course',
    })
  },
  scanCodeEvent() {
    const name = this.data.userName;
    wx.scanCode({
      onlyFromCamera: false,// 只允许从相机扫码
      success: async (res) => {
        const data = res.result
        const enc_start = data.indexOf('&enc=') + 5
        let enc = data.slice(enc_start, data.indexOf('&', enc_start))
        const courses = await courseApi.getCoursesLocal();
        if (typeof courses == 'string') {
          showToast({ title: '获取课程活动失败', icon: 'error' })
          return;
        }
        signActivity.qRCodeSignAll(courses, enc, name)
      }
    })
  },
  async loginOut(){
    const phone = userApi.getCurUser();
    await dbUserApi.setOrUpdateConfigByUsername({
      _id:phone,
      _openid:"",
    })
    userApi.loginOut(phone)
    wx.navigateTo({url:"../login/login"})
  },
  onShow() {
    this.setData({ userName: userApi.getUserRealName() })
  },
})